/* 考试信息管理 */
import React, { PureComponent } from 'react'
import { Tabs, Card } from 'antd';
import { PageContainer } from '@ant-design/pro-layout';
import { history } from 'umi'
import { SettingOutlined, RadarChartOutlined, BlockOutlined, FileExcelOutlined, StopOutlined } from '@ant-design/icons';
import StatisticalAnalysis from './StatisticalAnalysis'
import BankManagement from './BankManagement'
import Excel from './Excel'
import BatchManagement from './BatchManagement'
import DisabledBank from './DisabledBank'
import { getSubject } from '../../services/lessons'
import Authorized from '../../components/Authorized/Authorized'
import { isCustome } from '../../utils/utils';

export default class Index extends PureComponent<any, any> {
    constructor(props: any) {
        super(props);
        this.state = {
            currentTab: window.location.hash[window.location.hash.length - 1],
            subjectTreeData: [],
            subjectOptions: [],
            tablist: []
        };
    }
    changeTabHandle = (e: string, params: any) => {
        this.setState({
            currentTab: e
        })
        if (params) {
            if (this.props.location.pathname.indexOf('tzExamInfo') === -1) {
                history.push(`/examInfo/questionBank/${e}?id=${params.id}`)
            } else {
                history.push(`/tzExamInfo/questionBank/${e}?id=${params.id}`)
            }
        } else {
            if (this.props.location.pathname.indexOf('tzExamInfo') === -1) {
                history.push('/examInfo/questionBank/' + e)
            } else {
                history.push('/tzExamInfo/questionBank/' + e)
            }
        }
    }
    componentDidMount() {
        const currentTab = this.props.match.params.tab === ':tab' ? '1' : this.props.match.params.tab
        this.setState({
            currentTab
        })
        const Super =  JSON.parse(localStorage.getItem('userinfo') || '{}').super;
        getSubject().then((response) => {
            if (!response || !response[0]) return;
            let res = [];
            if(isCustome()&&!Super){
                for (let i = 0; i < response.length; i++) {
                    const item = response[i];
                    if(item.id===250){
                        res.push(response[i])
                    }
                }
            }else{
                res=response;
            }
            let data = JSON.stringify(res);
            let data2 = JSON.stringify(res);
            data = data.replace(/id/g, 'key').replace(/pkey/g, 'pid');
            data2 = data2
                .replace(/id/g, 'value')
                .replace(/title/g, 'label')
                .replace(/pvalue/g, 'pid');
            let subjectTreeData =  JSON.parse(data)
            let subjectOptions =  JSON.parse(data2)
          
            this.setState({
                subjectTreeData,
                subjectOptions,
            });
        });
        let tablist = [
            { title: '管理题库信息', id: '1', authority: [301, 99996], icon: <SettingOutlined /> },
            { title: '统计分析题库', id: '2', authority: [307, 999992], icon: <RadarChartOutlined /> },
            { title: 'Excel批量导入', id: '3', authority: [306, 999991], icon: <FileExcelOutlined /> },
            { title: '批量管理题库', id: '4', authority: [306, 999991], icon: <BlockOutlined /> },
            { title: '禁用题库', id: '7', authority: [308, 999993], icon: <StopOutlined /> },
        ]
        let newTabList = [];
        const authority =  JSON.parse(localStorage.getItem('antd-pro-authority') || '');

        for (let i = 0; i < tablist.length; i++) {
            const item = tablist[i];
            if (location.href.includes('tzExamInfo')) {
                if (authority.includes(item.authority[1])) {
                    newTabList.push(item)
                }
            } else {
                if (authority.includes(item.authority[0])) {
                    newTabList.push(item)
                }
            }
            // for (let j = 0; j < authority.length; j++) {
            //     const a = authority[j];
            //     if(location.href.indexOf('tzExamInfo')!==-1){
            //         console.log(item)
            //         if (item.authority[1] === a) {
            //             newTabList.push(item)
            //         }
            //     }else{
            //         if (item.authority[0]===a) {
            //             newTabList.push(item)
            //         }
            //     }

            // }
        }
        this.setState({
            tablist: newTabList,
            currentTab: newTabList[0] ? newTabList[0].id : '1'
        })
    }
    render() {
        const { currentTab, subjectTreeData, subjectOptions, tablist } = this.state;
        const { history } = this.props;

        const renderChild = (currentTab: string) => {
            switch (currentTab) {
                case '2':
                    return <Authorized authority={[307, 999992]}><StatisticalAnalysis changeTabHandle={this.changeTabHandle} /></Authorized>;
                case '3':
                    return <Authorized authority={[306, 999991]}><Excel subjectOptions={subjectOptions} /></Authorized>
                case '4':
                    return <Authorized authority={[306, 999991]}><BatchManagement subjectOptions={subjectOptions} /></Authorized>
                case '7':
                    return <Authorized authority={[308, 999993]}><DisabledBank subjectTreeData={subjectTreeData} history={history} subjectOptions={subjectOptions} /></Authorized>
                default:
                    return <Authorized authority={[301, 99996]}><BankManagement subjectTreeData={subjectTreeData} history={history} subjectOptions={subjectOptions} /></Authorized>
            }
        }
        return (
            <PageContainer>
                <Card>
                    <Tabs onChange={this.changeTabHandle} activeKey={currentTab}>
                        {
                            tablist.map((item: any) => {
                                return <Tabs.TabPane key={item.id} tab={<Authorized authority={item.authority}><span>{item.icon}{item.title}</span></Authorized>}></Tabs.TabPane>
                            })
                        }
                    </Tabs>
                    {renderChild(currentTab)}
                </Card>
            </PageContainer>
        )
    }
}
